

# -*- coding: utf-8; py-indent-offset:4 -*-
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from sma import sma_cross
from sma_add_trail_order import sma_cross_add_trail
from bol import boll_cross
import datetime  # For datetime objects
import os.path  # To manage paths
# import sys  # To find out the script name (in argv[0])
import numpy
import pandas as pd

import backtrader as bt
import backtrader.indicators as btind


if __name__ == '__main__':
    # general run strategy
    cerebro = bt.Cerebro()
    cerebro.addwriter(bt.WriterFile, csv=True, out='../backtesting_csv_result/strategy.csv')
    cerebro.addobserver(bt.observers.DrawDown)
    cerebro.addobserver(bt.observers.Benchmark)
    cerebro.addobserver(bt.observers.Trades)
    cerebro.addobserver(bt.observers.TimeReturn)
    cerebro.addobserver(bt.observers.DataTrades)

    cerebro.addstrategy(sma_cross)
    cerebro.addstrategy(boll_cross)

    dataframe = pd.read_csv('../rbi.csv', index_col=0, parse_dates=True)
    dataframe['openinterest'] = 0
    data = bt.feeds.PandasData(dataname=dataframe)
    # Add the Data Feed to Cerebro
    cerebro.adddata(data)
    # cerebro.adddata(data)

    # Set our desired cash start
    cerebro.broker.setcash(100000.0)
    cerebro.broker.setcommission(commission=4,
                                 mult=10,
                                 margin=0.1)
    # Print out the starting conditions
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

    cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='SharpeRatio')
    cerebro.addanalyzer(bt.analyzers.DrawDown, _name='DrawDown')
    cerebro.addanalyzer(bt.analyzers.AnnualReturn, _name='AnnualReturn')
    cerebro.addanalyzer(bt.analyzers.Calmar, _name='Calmar') # it is needed time frame?
    cerebro.addanalyzer(bt.analyzers.Returns, _name='Returns')
    cerebro.addanalyzer(bt.analyzers.SQN, _name='SQN')
    cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name='TradeAnalyzer')
    cerebro.addanalyzer(bt.analyzers.Transactions, _name='Transactions')
    cerebro.addanalyzer(bt.analyzers.VWR, _name='VWR')


    results = cerebro.run()
    strat = results[0]
    print('sharpe_ratio:', strat.analyzers.SharpeRatio.get_analysis())
    print('max_draw_down', strat.analyzers.DrawDown.get_analysis()['max']['drawdown'])
    print('annual_return', strat.analyzers.AnnualReturn.get_analysis())
    print('total_return', strat.analyzers.Returns.get_analysis()['rtot'])
    print('sqn', strat.analyzers.SQN.get_analysis()['sqn'])
    # print('trades', strat.analyzers.SQN.get_analysis()['trades'])
    print('ver', strat.analyzers.VWR.get_analysis()['vwr'])


    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    cerebro.plot()


    # optimization for the strategy

    # cerebro = bt.Cerebro()
    # cerebro.optstrategy(
    #     sma_cross,
    #     sma_window=range(5, 80))
    # dataframe = pd.read_csv('rbi.csv', index_col=0, parse_dates=True)
    # dataframe['openinterest'] = 0
    # data = bt.feeds.PandasData(dataname=dataframe
    #                            )
    # cerebro.adddata(data)
    # cerebro.broker.setcash(100000.0)
    # cerebro.broker.setcommission(commission=4,
    #                              mult=10,
    #                              margin=0.1)
    #
    # cerebro.run()